package com.org.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * jugui
 */
public class DateUtils {

    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 获取指定日期加固定月
     *
     * @param startDate
     * @param afterMonths
     * @return
     */
    public static Date getDateAfterMonths(Date startDate, int afterMonths) {
        if (null == startDate) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        // 起点日期
        calendar.setTime(startDate);
        calendar.add(Calendar.MONTH, afterMonths);

        return calendar.getTime();
    }

    /**
     * 获取指定日期加固定天
     *
     * @param startDate
     * @param afterDays
     * @return
     */
    public static Date getDateAfterDays(Date startDate, int afterDays) {
        if (null == startDate) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        // 起点日期
        calendar.setTime(startDate);
        calendar.add(Calendar.DATE, afterDays);

        return calendar.getTime();
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int getDiffDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                //闰年
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    timeDistance += 366;
                } else { //不是闰年
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else { //不同年
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    /**
     * 获取指定日期前days天的日期
     *
     * @param days
     * @param startDate
     * @return
     */
    public static Date getDateBeforeDays(int days, Date startDate) {
        if (null == startDate) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        // 起点日期
        calendar.setTime(startDate);
        // 过去days天
        calendar.add(Calendar.DATE, -days);

        return calendar.getTime();
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static String getNowTime() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_PATTERN);
        return df.format(date);
    }

    /**
     * 获取当前日期 yyyy-MM-dd
     *
     * @return
     */
    public static Date getNowDate() {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        // 将时分秒,毫秒域清零
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 获取两个日期之间相差的年数月数日数
     *
     * @param dateStart
     * @param dateEnd
     * @return 相差年数，月数，天数
     */
    public static int[] getDiffBetweenTwoDate(Date dateStart, Date dateEnd) {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(dateStart);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(dateEnd);
        int diffYears = 0, diffMonths, diffDays;
        int dayOfBirth = calendarStart.get(Calendar.DAY_OF_MONTH);
        int dayOfNow = calendarEnd.get(Calendar.DAY_OF_MONTH);
        if (dayOfBirth <= dayOfNow) {
            diffMonths = getMonthsOfAge(calendarStart, calendarEnd);
            diffDays = dayOfNow - dayOfBirth;
            if (diffMonths == 0)
                diffDays++;
        } else {
            if (isEndOfMonth(calendarStart)) {
                if (isEndOfMonth(calendarEnd)) {
                    diffMonths = getMonthsOfAge(calendarStart, calendarEnd);
                    diffDays = 0;
                } else {
                    calendarEnd.add(Calendar.MONTH, -1);
                    diffMonths = getMonthsOfAge(calendarStart, calendarEnd);
                    diffDays = dayOfNow + 1;
                }
            } else {
                if (isEndOfMonth(calendarEnd)) {
                    diffMonths = getMonthsOfAge(calendarStart, calendarEnd);
                    diffDays = 0;
                } else {
                    calendarEnd.add(Calendar.MONTH, -1);// 上个月
                    diffMonths = getMonthsOfAge(calendarStart, calendarEnd);
                    // 获取上个月最大的一天
                    int maxDayOfLastMonth = calendarEnd
                            .getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (maxDayOfLastMonth > dayOfBirth) {
                        diffDays = maxDayOfLastMonth - dayOfBirth + dayOfNow;
                    } else {
                        diffDays = dayOfNow;
                    }
                }
            }
        }

        // 计算月份时，没有考虑年
        diffYears = diffMonths / 12;
        diffMonths = diffMonths % 12;
        return new int[]{diffYears, diffMonths, diffDays};

    }

    /**
     * @param calendarStart
     * @param calendarEnd
     * @return
     */
    public static int getMonthsOfAge(Calendar calendarStart,
                                     Calendar calendarEnd) {
        return (calendarEnd.get(Calendar.YEAR) - calendarStart
                .get(Calendar.YEAR))
                * 12
                + calendarEnd.get(Calendar.MONTH)
                - calendarStart.get(Calendar.MONTH);
    }

    /**
     * 判断这一天是否是月底
     *
     * @param calendar
     * @return
     */
    public static boolean isEndOfMonth(Calendar calendar) {
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        if (dayOfMonth == calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
            return true;
        return false;
    }

}
